<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.net.*" %>
<%
String namestr=request.getParameter("name");
String urlstr=request.getParameter("url");
//设置name和url
Cookie name=new Cookie("name",namestr);
Cookie url=new Cookie("url",urlstr);
//设置cookie过期时间
name.setMaxAge(5);
url.setMaxAge(10);

//在响应头部添加cookie
response.addCookie(name);
response.addCookie(url);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>设置cookie</title>
</head>
<body>
<h1>设置cookie</h1>
如果cookie过期时间等于0，也就是setMaxAge(0)表示立即失效
<ul>
<li>
<p><b>网站名：</b>
<%=request.getParameter("name") %>
</p>
</li>
<li>
<p><b>网址：</b>
<%=request.getParameter("url") %>
</p>
</li>
</ul>
<a href="cookieread.jsp">查看cookie</a>
<a href="javascript:history.go(-1);">返回</a>
</body>
</html>